﻿using JZ.JiangSu.Common;
using JZ.JiangSu.IApplication.IServices;
using JZ.JiangSu.IRepository;
using JZ.JiangSu.Model.Entity;
using JZ.JiangSu.Repository;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using DependencyAttribute = Unity.Attributes.DependencyAttribute;

namespace JZ.JiangSu.Application.Services
{
    public class ApplicationService<T>:IApplicationService<T> where T:BaseEntity
    {
        private ILog logger;
        /// <summary>
        /// 日志
        /// </summary>
        public ILog Logger
        {
            get
            {
                if (logger == null)
                    logger = LoggerManager.GetLogger(this.GetType());
                return logger;
            }
        }


        [Dependency]
        public IBaseRepository<T> _repository { get; set; }
        //public readonly IBaseRepository<T> _repository = new BaseRepository<T>();
        public int Add(T entity)
        {
            return _repository.Add(entity);
        }
        public int Delete(int id)
        {
            return _repository.Delete(id);
        }
        public int Delete(IList<int> idList)
        {
            return _repository.Delete(idList);
        }
        public int Update(T entity)
        {
            if (entity == null)
            {
                return 0;
            }
            return _repository.Update(entity);
        }
        public int Update(int entityId, Dictionary<string, object> modifyFields)
        {
            //检查字段名是否正确
            List<string> fieldsList = new List<string>();
            Type type = typeof(T);
            foreach (PropertyInfo propertyInfo in type.GetProperties())
            {
                if (propertyInfo.GetGetMethod().IsVirtual)
                {
                    continue;
                }
                fieldsList.Add(propertyInfo.Name.ToLower());
            }
            if (modifyFields.Any(field => string.IsNullOrEmpty(field.Key) || !fieldsList.Contains(field.Key.ToLower())))
            {
                return -1;
                //throw new Exception("要修改的字段名不能为空,并且字段名必须与表中列名一致");
            }
            return _repository.Update(new List<int>() { entityId }, modifyFields);
        }

        public int Update(IList<int> entityIdList, Dictionary<string, object> modifyFields)
        {
            //检查字段名是否正确
            List<string> fieldsList = new List<string>();
            Type type = typeof(T);
            foreach (PropertyInfo propertyInfo in type.GetProperties())
            {
                if (propertyInfo.GetGetMethod().IsVirtual)
                {
                    continue;
                }
                fieldsList.Add(propertyInfo.Name);
            }
            if (modifyFields.Any(field => string.IsNullOrEmpty(field.Key) || !fieldsList.Contains(field.Key)))
            {
                return -1;
                //throw new Exception("要修改的字段名不能为空,并且字段名必须与表中列名一致");
            }
            return _repository.Update(entityIdList, modifyFields);
        }

        public T GetEntityById(int entityId)
        {
            return _repository.GetEntityById(entityId);
        }
        public List<T> GetAllEntities()
        {
            return _repository.GetAllEntities();
        }
        public List<T> GetEntitiesByIds(IList<int> idList)
        {
            return _repository.GetEntitiesByIds(idList);
        }
    }
}
